const app = getApp()
import {
  getMsgUserList
} from '../api/ChatMsgController.js'
Component({
  data: {
    selected: 0,
    color: "#777777",
    count: 99,
    list: [{
        text: "首页",
        pagePath: "/pages/index/index",
        iconPath: "/static/tabbar/index.svg",
        selectedIconPath: "/static/tabbar/index-active.svg"
      },
      {
        text: "好事圈",
        pagePath: "/pages/village/index",
        iconPath: "/static/tabbar/village.svg",
        selectedIconPath: "/static/tabbar/village-active.svg"
      },
      {
        pagePath: "/pages/publish/index",
        bulge: true,
        text: "发布"
      },
      {
        text: "消息",
        corners: true,
        pagePath: "/pages/message/index",
        iconPath: "/static/tabbar/message.svg",
        selectedIconPath: "/static/tabbar/message-active.svg"
      },
      {
        text: "我的",
        pagePath: "/pages/my/index",
        iconPath: "/static/tabbar/my.svg",
        selectedIconPath: "/static/tabbar/my-active.svg"
      },
    ],
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({
        url
      })
    },
    //获取用户消息数量
    async getCount(userId) {
      const res = await getMsgUserList({
        pageNum: 1,
        pageSize: 20,
        userId
      })
      this.setData({
        count: res.list.reduce((sum, ele) => +ele.unreadCount + sum, 0)
      })
      app.count = this.data.count
    },
  },
  lifetimes: {
    attached() {
      wx.getStorageSync('userId') && this.getCount(wx.getStorageSync('userId'))
      this.setData({
        count: app.count
      })
    }
  }
});